
package com.e885.util.web;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 當我們發生 Exception 時, 或者發生錯誤時, 我們都會由 log 或 console 要了解發生什麼錯誤, <br>
 * 但有些公司有資訊安全上的顧慮, 所以無法隨時隨地進去伺服器去了解這些 log, <br>
 * 所以可藉由這個 class , 再搭配 mail 的方式, 便可解決這種方式
 * 
 * @author Grady.Kao
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2000-2008
 */

public class Report
{
	/**
	 * make report
	 * 
	 * @param request
	 * @param throwable
	 * @return
	 */
	public String makeReport( HttpServletRequest request, Throwable throwable )
	{
		StringBuffer buffer = new StringBuffer();

		reportException( buffer, throwable );
		reportRequest( buffer, request );
		reportParameter( buffer, request );
		reportHeader( buffer, request );
		reportCookie( buffer, request );

		return buffer.toString();
	}

	/**
	 * exception report
	 * 
	 * @param buffer
	 * @param throwable
	 */
	public void reportException( StringBuffer buffer, Throwable throwable )
	{
		buffer.append( "\n" );

		StringWriter writer = new StringWriter();

		throwable.printStackTrace( new PrintWriter( writer ) );

		buffer.append( writer.getBuffer() );
	}

	/**
	 * header report
	 * 
	 * @param buffer
	 * @param request
	 */
	public void reportHeader( StringBuffer buffer, HttpServletRequest request )
	{
		Enumeration enumeration = request.getHeaderNames();

		if( !enumeration.hasMoreElements() )
			return;

		buffer.append( "\n" );
		buffer.append( "Headers:\n" );

		while( enumeration.hasMoreElements() )
		{
			String name = ( String )enumeration.nextElement();

			String value = request.getHeader( name );

			buffer.append( "     " );
			buffer.append( name );
			buffer.append( "=" );
			buffer.append( value );

			buffer.append( "\n" );
		}
	}

	/**
	 * cookie report
	 * 
	 * @param buffer
	 * @param request
	 */
	public void reportCookie( StringBuffer buffer, HttpServletRequest request )
	{
		Cookie[] cookies = request.getCookies();

		if( cookies == null )
			return;

		int length = cookies.length;

		if( length == 0 )
			return;

		buffer.append( "\n" );
		buffer.append( "Cookies:\n" );

		for( int i = 0; i < length; i++ )
		{
			buffer.append( "     " );
			buffer.append( cookies[ i ].getName() );
			buffer.append( "=" );
			buffer.append( cookies[ i ].getValue() );

			buffer.append( "\n" );
		}
	}

	/**
	 * parameter report
	 * 
	 * @param buffer
	 * @param request
	 */
	public void reportParameter( StringBuffer buffer, HttpServletRequest request )
	{
		Enumeration enumeration = request.getParameterNames();

		if( !enumeration.hasMoreElements() )
			return;

		buffer.append( "\n" );
		buffer.append( "Parameters:\n" );

		while( enumeration.hasMoreElements() )
		{
			String name = ( String )enumeration.nextElement();

			String[] values = request.getParameterValues( name );

			int length = values.length;

			buffer.append( "     " );
			buffer.append( name );
			buffer.append( "=" );

			if( length >= 1 )
				buffer.append( values[ 0 ] );

			for( int i = 1; i < length; i++ )
			{
				buffer.append( "," );
				buffer.append( values[ i ] );
			}

			buffer.append( "\n" );
		}
	}

	/**
	 * request report
	 * 
	 * @param buffer
	 * @param request
	 */
	public void reportRequest( StringBuffer buffer, HttpServletRequest request )
	{
		buffer.append( "\n" );
		reportRequestURL( buffer, request );

		buffer.append( "\n" );
		reportRequestSessionId( buffer, request );
	}

	/**
	 * about cookie report
	 * 
	 * @param buffer
	 * @param request
	 */
	private void reportRequestSessionId( StringBuffer buffer, HttpServletRequest request )
	{
		buffer.append( "Session ID:" );

		String sessionId = request.getRequestedSessionId();

		if( sessionId == null )
			buffer.append( "none" );
		else
		{
			if( request.isRequestedSessionIdValid() )
			{
				buffer.append( sessionId );
				buffer.append( "(from " );

				if( request.isRequestedSessionIdFromURL() )
					buffer.append( "url" );
				else if( request.isRequestedSessionIdFromCookie() )
					buffer.append( "cookie" );
				else
					buffer.append( "unkown" );

				buffer.append( ")" );
			}
			else
			{
				buffer.append( "invalid" );
			}
		}
	}

	/**
	 * about url report
	 * 
	 * @param buffer
	 * @param request
	 */
	private void reportRequestURL( StringBuffer buffer, HttpServletRequest request )
	{
		buffer.append( "Request:" );
		buffer.append( request.getMethod() );
		buffer.append( " " );
		buffer.append( request.getRequestURL() );

		String queryString = request.getQueryString();
		if( queryString != null )
		{
			buffer.append( "?" );
			buffer.append( queryString );
		}
	}
}
